package com.webvihar.access2db.activities;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.webvihar.access2db.R;
import com.webvihar.access2db.adapters.SessionAdapter;
import com.webvihar.access2db.model.Session;
import com.webvihar.access2db.utils.PreferencesManager;
import com.webvihar.common.db.DbService;

public class SessionsActivity extends BaseActivity implements OnClickListener, OnItemClickListener{

	private String SESSION_SESSION_NAME = "SessionName";
	private String SESSION_SESSION_HOST = "SessionHost";
	private String SESSION_SESSION_PORT = "SessionPort";
	private String SESSION_SESSION_USERNAME = "SessionUsername";
	private String SESSION_SESSION_PASSWORD = "SessionPassword";
	
	private DbService db;
	private ListView sessionList;
	private TextView titleTxt;
	private ImageButton addButton;
	private List<Session> sessions;

	/** Called when the activity is first created. **/
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
		setContentView(R.layout.sessions);
		initDbService();
		setTitleBar();
		initUI();
		setListeners();
		setAdapter();
		registerContext();
	}

	private void registerContext() {
		registerForContextMenu(sessionList);
	}

	@Override
	protected void onResume() {
		super.onResume();
		setAdapter();
	}


	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		if(v.getId() == R.id.sessions_session_lst) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
			Session session = getData().get(info.position);
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.context_menu, menu);
			menu.setHeaderTitle(session.sessionName);
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.menu_edit_item:
			Session session = getData().get(info.position);
			Intent intent = new Intent(SessionsActivity.this, EditSessionActivity.class);
			intent.putExtra(SESSION_SESSION_NAME, session.sessionName);
			intent.putExtra(SESSION_SESSION_HOST, session.host);
			intent.putExtra(SESSION_SESSION_PORT, session.port);
			intent.putExtra(SESSION_SESSION_USERNAME, session.username);
			intent.putExtra(SESSION_SESSION_PASSWORD, session.password);
			startActivity(intent);
			return(true);
		case R.id.menu_delete_item:
			Session session1 = getData().get(info.position);
			Intent intent1 = new Intent(SessionsActivity.this, DeleteSessionActivity.class);
			intent1.putExtra(SESSION_SESSION_NAME, session1.sessionName);
			intent1.putExtra(SESSION_SESSION_HOST, session1.host);
			intent1.putExtra(SESSION_SESSION_PORT, session1.port);
			intent1.putExtra(SESSION_SESSION_USERNAME, session1.username);
			intent1.putExtra(SESSION_SESSION_PASSWORD, session1.password);
			startActivity(intent1);
			return(true);
		default:
			return(super.onOptionsItemSelected(item));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.database_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case R.id.menu_database_refresh:
			setAdapter();
			break;
		case R.id.menu_database_query_editor:
			navigateToQueryEditor();
			break;
		default:
			break;
		}
		return true;
	}

	private void navigateToQueryEditor() {
		Intent intent = new Intent(SessionsActivity.this, QueryEditorActivity.class);
		startActivity(intent);		
	}

	private void setTitleBar() {
		titleTxt = (TextView) findViewById(R.id.title_txt);
		titleTxt.setText(getString(R.string.sessions_txt));
		addButton = (ImageButton) findViewById(R.id.title_action_image_btn);
		addButton.setVisibility(View.VISIBLE);
	}

	private void initUI() {
		sessionList = (ListView)findViewById(R.id.sessions_session_lst);
	}

	private void setListeners() {
		addButton.setOnClickListener(this);
		sessionList.setOnItemClickListener(this);
	}

	private void initDbService() {
		db = new DbService(this);
	}

	private void setAdapter() {
		sessionList.setAdapter(new SessionAdapter(this, getData()));
		sessionList.invalidate();
	}


	public List<Session> getData() {
		if(sessions == null) {
			sessions = (List<Session>)db.Get(Session.class);
		}
		return sessions;
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(SessionsActivity.this, AddSessionActivity.class);
		startActivity(intent);
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
		Session session = (Session) adapter.getItemAtPosition(position);
		addSessionToPreferences(session);
		startActivity(new Intent(SessionsActivity.this, DatabaseActivity.class));
	}

	public void addSessionToPreferences(Session session) {		
		PreferencesManager.setString(this, PREF_SESSION_NAME, session.sessionName);
		PreferencesManager.setString(this, PREF_SESSION_HOST, session.host);
		PreferencesManager.setString(this, PREF_SESSION_PORT, session.port);
		PreferencesManager.setString(this, PREF_SESSION_USERNAME, session.username);
		PreferencesManager.setString(this, PREF_SESSION_PASSWORD, session.password);
		PreferencesManager.setString(this, PREF_SESSION_DBNAME, getDefualtDatabase());
	}
}
